package com.cvmars.jianyou.module.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cvmars.jianyou.R;
import com.cvmars.jianyou.model.UserInfoManager;
import com.cvmars.jianyou.module.activity.PersonHomeActivity;
import com.cvmars.jianyou.module.model.FindCommentModel;
import com.cvmars.jianyou.utils.ImageUtils;
import com.cvmars.jianyou.utils.UtilHelper;

import java.util.List;

/**
 * Created by hehaifeng on 2018/12/24.
 */

public class FindCommentAdapter extends BaseAdapter {

    List<FindCommentModel> commentModels;

    public void setCommentModels(List<FindCommentModel> commentModels) {
        this.commentModels = commentModels;
        notifyDataSetChanged();
    }

    public FindCommentAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return commentModels != null ? commentModels.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return commentModels.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {


        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_comment, null);
            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.txt_name);
            holder.txtContent = convertView.findViewById(R.id.txt_intro);
            holder.time = convertView.findViewById(R.id.txt_time);
            holder.imageView = convertView.findViewById(R.id.iv_avator);
            holder.ll_reply = convertView.findViewById(R.id.ll_reply);
            holder.txt_replay = convertView.findViewById(R.id.txt_replay);
            holder.iv_delete = convertView.findViewById(R.id.iv_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FindCommentModel model = commentModels.get(i);

        holder.textView.setText(model.from_customer.name);
        if (model.to_customer != null) {
            holder.txt_replay.setText(model.to_customer.name);
            holder.ll_reply.setVisibility(View.VISIBLE);
        } else {
            holder.ll_reply.setVisibility(View.GONE);
        }

        ImageUtils.loadImage(mContext, model.from_customer.avatar_url, holder.imageView);
        holder.txtContent.setText(model.text);
        holder.iv_delete.setVisibility(UserInfoManager.getInstance()
                .isOneSelf(model.from_customer.id) ? View.VISIBLE : View.GONE);
        holder.time.setText(UtilHelper.forumFriendlyTime(model.create_at));

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(mContext, PersonHomeActivity.class);
                intent.putExtra("userid", model.from_customer.id);
                mContext.startActivity(intent);
            }
        });
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                new AlertDialog.Builder(mContext).setMessage("是否删除评论？").setTitle("")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {


                                if (onDelete != null) {

                                    onDelete.onDelete(model.id + "");
                                }

                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                }).show();
            }
        });
        return convertView;
    }


    OnClickDeleteListener onDelete;

    public void setOnDelete(OnClickDeleteListener onDelete) {
        this.onDelete = onDelete;
    }

    public interface OnClickDeleteListener {

        void onDelete(String commID);
    }


    public Context mContext;

    static class ViewHolder {
        TextView textView;
        TextView txt_replay;
        TextView txtContent;
        TextView time;
        ImageView imageView;
        ImageView iv_delete;
        LinearLayout ll_reply;
    }
}
